package com.yswl.yeeduo.ui.activity;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.adapter.BannerViewPagerAdapter;
import com.yswl.yeeduo.ui.adapter.RankingListAdapter;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.bean.BannerBean;
import com.yswl.yeeduo.ui.bean.RankingBean;
import com.yswl.yeeduo.ui.bean.RankingBean.ActiveListItemBean;
import com.yswl.yeeduo.ui.task.BannerTask;
import com.yswl.yeeduo.ui.task.RankingTask;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.view.CommonEmptyView;
import com.yswl.yeeduo.view.listview.BaseListView;

/**
 * 排行榜列表
 * 
 * @author Administrator
 */
public class RankingListActivity extends BaseActivity implements TaskDelegate {

	RankingListAdapter adapter;
	RankingListAdapter adapter2;
	private ScheduledExecutorService scheduledExecutorService;// 定时周期执行指定任务
	private ViewPager mViewpager;
	private int currentPage = -1;// 当前页
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			mViewpager.setCurrentItem(currentPage);
		};
	};

	private void initViewPager(final ArrayList<BannerBean> list) {
		BannerViewPagerAdapter mAdapter = new BannerViewPagerAdapter(this, list);
		mViewpager.setAdapter(mAdapter);
		scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				synchronized (mViewpager) {
					handler.sendEmptyMessage(++currentPage);
				}
			}
		}, 1000, 5000, TimeUnit.MILLISECONDS);

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_rankinglist);
		super.setHeader("排行榜");

		initUI();
		getData();
	}

	TextView mShareCount;
	TextView mRanking;
	TextView mRanking2;
	BaseListView ranking_list;
	BaseListView ranking_over_list;

	private void initUI() {
		mViewpager = (ViewPager) findViewById(R.id.viewpager_banner);
		mShareCount = (TextView) findViewById(R.id.share_count_tv);
		mRanking = (TextView) findViewById(R.id.ranking10_tv);
		mRanking2 = (TextView) findViewById(R.id.ranking100_tv);
		ranking_list = (BaseListView) findViewById(R.id.rangking_list);
		ranking_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ActiveListItemBean bean = adapter.getItem(position);
				if (bean != null) {
					startActivity(RankingListDetailActivity.createIntent(RankingListActivity.this, bean.id));
				}
			}
		});
		ranking_over_list = (BaseListView) findViewById(R.id.rangking_list_over);
		ranking_over_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ActiveListItemBean bean = adapter2.getItem(position);
				if (bean != null) {
					JpushWebViewActivity.actionStart(RankingListActivity.this, bean.id);
				}
			}
		});
		CommonEmptyView.setCommonEmptyView(this, ranking_list, null);
		CommonEmptyView.setCommonEmptyView(this, ranking_over_list, null);
	}

	private void getData() {
		new BannerTask(this, 10222, this).execute();// 获取轮播图片
		Map<String, String> params = new TreeMap<String, String>();
		String userId = ServiceAuthManager.getUserId(this);
		if (KStringTool.isEmpty(userId)) {
			userId = "-1";
		}
		params.put("userId", userId);
		params.put("api_key", ServiceAuthManager.getToken(this));
		new RankingTask(this, 10333, this).execute(params);// 获取排行榜数据
	}

	// Um
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName());
		MobclickAgent.onResume(this); // 统计时长
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
		MobclickAgent.onPause(this);
	}

	@Override
	public void faild_TaskDelegate(int taskId, Object object) {

	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		if (taskId == 10222) {
			ArrayList<BannerBean> list = (ArrayList<BannerBean>) object;
			ArrayList<BannerBean> newlist = new ArrayList<BannerBean>();
			newlist.add(list.get(0));		
			initViewPager(newlist);
		} else if (taskId == 10333) {
			RankingBean bean = (RankingBean) object;
			mShareCount.setText(bean.totalShare);
			mRanking.setText(bean.top10);
			mRanking2.setText(bean.top100);
			adapter = new RankingListAdapter(this);
			adapter.setData((ArrayList<ActiveListItemBean>) bean.activeList);
			ranking_list.setAdapter(adapter);
			adapter2 = new RankingListAdapter(this);
			adapter2.setData((ArrayList<ActiveListItemBean>) bean.finishList);
			ranking_over_list.setAdapter(adapter2);
		}
	}

}
